package fr.resasante.utils;

import java.io.InputStream;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.naming.NamingException;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import fr.resasante.exception.ApplicationException;

/**
 * Class for getting specialities in XML
 * 
 * @author GEFFROY-DE-VILLEBLAN
 * 
 */
public final class DepartmentsUtils {
	/**
	 * Properties class instance
	 */
	private static volatile DepartmentsUtils instance = null;

	/**
	 * Root element
	 */
	private Element root;

	/**
	 * Constructor
	 */
	private DepartmentsUtils() {
		super();
		loadXmlFile();
	}

	/**
	 * Method returning a Singleton class instance
	 * 
	 * @return Singleton instance
	 * @throws NamingException
	 */
	public final static DepartmentsUtils getInstance() {
		if (DepartmentsUtils.instance == null) {
			synchronized (DepartmentsUtils.class) {
				if (DepartmentsUtils.instance == null) {
					DepartmentsUtils.instance = new DepartmentsUtils();
				}
			}
		}
		return DepartmentsUtils.instance;
	}

	/**
	 * Load the XML file
	 */
	private void loadXmlFile() {
		try {
			InputStream xmlStream = this.getClass().getClassLoader()
					.getResourceAsStream("departments.xml");
			SAXReader reader = new SAXReader();
			Document doc = reader.read(xmlStream);
			root = doc.getRootElement();
		} catch (Exception e) {
			throw new ApplicationException("Unable to find the XML file departments.xml", e);
		}
	}

	/**
	 * Check if the departement label and code exist
	 * 
	 * @param code
	 *            The departement code
	 * @param label
	 *            The department label
	 * @return true if the departement exists, false if it doesn't exists
	 */
	public Boolean isDepartmentExists(String code, String label) {
		@SuppressWarnings("rawtypes")
		Iterator iterator = root.elementIterator("department");
		while (iterator.hasNext()) {
			Element department = (Element) iterator.next();
			String xmlCode = department.elementText("code");
			String xmlLabel = department.elementText("label");
			if (code.equals(xmlCode) && label.equals(xmlLabel)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * Get a list of departments
	 * 
	 * @return The list of department
	 */
	public Map<String, String> getAllDepartments() {
		Map<String, String> departments = new LinkedHashMap<String, String>();

		@SuppressWarnings("rawtypes")
		Iterator iterator = root.elementIterator("department");
		while (iterator.hasNext()) {
			Element title = (Element) iterator.next();
			String code = title.elementText("code");
			String label = title.elementText("label");
			departments.put(code, label);
		}
		return departments;
	}

	/**
	 * Get a department label by its code
	 * 
	 * @param requestedCode
	 *            The requested code
	 * @return The department label
	 */
	public String getLabelDepartementByCode(String requestedCode) {
		if (null != requestedCode) {
			@SuppressWarnings("rawtypes")
			Iterator iterator = root.elementIterator("department");
			while (iterator.hasNext()) {
				Element title = (Element) iterator.next();
				String code = title.elementText("code");
				String label = title.elementText("label");
				if (requestedCode.equals(code)) {
					return label;
				}
			}
		}
		return "";
	}
}